package com.bsoft.gol.hcb.service;

import com.bsoft.gol.hcb.dao.MedicaltoolsCollectRecordDao;
import com.bsoft.gol.hcb.serviceEnum.MedicalToolsEnum;
import ctd.controller.exception.ControllerException;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.annotation.RpcService;
import hcn.base.MedicaltoolsCollectRecord;
import hcn.service.base.IMedicaltoolsCollectRecordService;
import hcn.service.base.qo.MedicaltoolsCollectRecordQo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.bsoft.gol.hcb.utils.CommonUtils;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @description:医学工具、公式收藏记录Service
 * @author: zhengcb10708
 * @create: 2021-07-29 11:13
 **/
@SsdevService("medicaltoolsCollectRecordService")
public class MedicaltoolsCollectRecordService implements IMedicaltoolsCollectRecordService {
    private static final Logger logger = LoggerFactory.getLogger(MedicaltoolsCollectRecordService.class);
    @Autowired
    private MedicaltoolsCollectRecordDao medicaltoolsCollectRecordDao;

    /**
     * @Describe: 收藏或者取消医学、工具公式接口
     * @Param: [request]
     * @Return: void
     * @Author: zhengcb10708
     * @Date: 2021/7/29 13:31
     */
    @RpcService
    @Override
    public void saveOrCancelMedicalTools(MedicaltoolsCollectRecordQo request) throws ControllerException {
        logger.info("saveOrCancelMedicalTools==入参："+request);
        if(CommonUtils.isEmpty(request.getUserId())||CommonUtils.isEmpty(request.getCalculatorCode())){
          throw new ControllerException("必要入参不能为空");
        }
        MedicalToolsEnum medicalToolsEnum =MedicalToolsEnum.get(request.getCalculatorCode());
        if(CommonUtils.isEmpty(medicalToolsEnum)){
            throw new ControllerException("非法的计算器代码，请检查");
        }
        try{
            Long count=medicaltoolsCollectRecordDao.getCountByCalculatorCodeAndUserId(request.getCalculatorCode(),request.getUserId());
            if(count<=0){//收藏
                MedicaltoolsCollectRecord record=new MedicaltoolsCollectRecord();
                record.setUserId(request.getUserId());
                record.setCalculatorCode(request.getCalculatorCode());
                record.setCalculatorName(medicalToolsEnum.getCalculatorName());
                record.setCalculatorParentCode(medicalToolsEnum.getCalculatorParentCode());
                record.setCalculatorParentName(medicalToolsEnum.getCalculatorParentName());
                //生成当前时间
                record.setCreateDate(new Timestamp(System.currentTimeMillis()));
                medicaltoolsCollectRecordDao.save(record);
            }else{//取消
                medicaltoolsCollectRecordDao.deleteMedicaltoolsCollectRecordByUserId(request.getCalculatorCode(),request.getUserId());
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("执行saveOrCancelMedicalTools出错 ，原因:"+e.getMessage());
            throw new ControllerException("收藏或者取消医学、工具失败");
        }
    }
    /**
     * @Describe: 通过计圈器code查询用户收藏记录
     * @Param: [request]
     * @Return: java.util.Map<java.lang.String,java.lang.Object>
     * @Author: zhengcb10708
     * @Date: 2021/7/30 13:42
     */
    @RpcService
    @Override
    public Map<String, Object> getMedicalToolsCollectByCode(MedicaltoolsCollectRecordQo request) throws ControllerException {
        logger.info("getMedicalToolsCollectByCode==入参："+request);
        if(CommonUtils.isEmpty(request.getUserId())){
            throw new ControllerException("必要入参不能为空");
        }
        return medicaltoolsCollectRecordDao.queryMedicaltoolsCollectRecordByUserId(request.getCalculatorCode(),request.getUserId());
    }
    /**
     * @Describe: 获取收藏列表
     * @Param: [request]
     * @Return: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @Author: zhengcb10708
     * @Date: 2021/7/30 13:42
     */
    @RpcService
    @Override
    public List<Map<String, Object>> queryMedicalToolsCollectList(MedicaltoolsCollectRecordQo request) throws ControllerException {
        logger.info("queryMedicalToolsCollectList==入参："+request);
        if(CommonUtils.isEmpty(request.getUserId())){
            throw new ControllerException("必要入参不能为空");
        }
        return medicaltoolsCollectRecordDao.queryMedicalToolsCollectList(request.getUserId(),request.getCalculatorCode(),request.getCalculatorName(),request.getPageNo(),request.getPageSize());
    }
}
